package com.guigu.chenjiajun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.chenjiajun.service.ProductionRegistrationAuditService;
import com.guigu.chenjiajun.vo.MManufactureVo;
import com.guigu.chenjiajun.vo.MProceduringVo;
import com.guigu.chenjiajun.vo.ProceduringVo;
import com.guigu.common.mapper.*;
import com.guigu.common.pojo.*;
import com.guigu.common.util.GetUUidUtil;
import com.guigu.common.vo.SearchVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductionRegistrationAuditServiceImpl implements ProductionRegistrationAuditService {


    @Resource
    private MManufactureMapper mManufactureMapper;

    @Resource
    private MProcedureModulingMapper mProcedureModulingMapper;

    @Resource
    private MProceduringMapper mProceduringMapper;

    @Resource
    private MProcedureMapper mProcedureMapper;

    @Resource
    private DFileMapper dFileMapper;

    @Resource
    private GetUUidUtil getUUidUtil;

    @Resource
    private SGatherMapper sGatherMapper;


    @Override
    public SearchVo<MManufacture> queryProductionAudit(SearchVo searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(), searchVo.getPageSize());
        PageInfo p = new PageInfo(this.mManufactureMapper.queryProductionAudit(searchVo));
        searchVo.setRows(p.getList());
        searchVo.setTotal(p.getTotal());
        return searchVo;
    }

    @Override
    public ProceduringVo checkInQueryProceduring(String id, String procedure_id) {
        ProceduringVo vo=this.mProceduringMapper.checkInQueryProceduring(id,procedure_id);
        List<MProcedureModuling> list=this.mProcedureModulingMapper.checkInQueryProceduring(vo.getId());
        vo.setProcedureModulingObj(list);
        return vo;
    }

    @Override
    public int submitAuditaa(ProceduringVo proceduringVo) {
        try {
            this.mManufactureMapper.updateCheckmanufacture_procedure_tag(proceduringVo.getManufactureId());
            this.mProceduringMapper.updateCheck(proceduringVo);
            this.mProceduringMapper.updateCheckTow(proceduringVo);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    public MProcedure TransferOfAudit(String id, String parentId) {
        return this.mProcedureMapper.TransferOfAudit(id,parentId);
    }

    @Override
    public int SubmitForHandoverReview(String manufactureId, String parentId, String procedureId) {
        try {
            this.mManufactureMapper.updateCheckmanufacture_procedure_tag(manufactureId);
            this.mProcedureMapper.SubmitForHandoverReview(parentId,procedureId);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    public int SubmitHandoverReviewAndWarehousing(MManufacture mManufacture) {
       try {
           this.mManufactureMapper.updateManufacturePriceSum(mManufacture);
           MManufacture mManuObj=this.mManufactureMapper.queryProcedureBymManufactureId(mManufacture.getManufactureId());
           DFile dFile = this.dFileMapper.queryById(mManuObj.getProductId());
           SGather sGather=new SGather();
           sGather.setGatherId(getUUidUtil.returnId("401"));
           sGather.setReason(mManufacture.getChecker());
           sGather.setReason("r001-1");
           sGather.setAmountSum(mManufacture.getTestedAmount());
           sGather.setCostPriceSum(mManufacture.getRealLabourCostPriceSum()+mManufacture.getRealModuleCostPriceSum());
           sGather.setGatheredAmountSum(mManufacture.getTestedAmount());
           sGather.setRemark(mManuObj.getRemark());
           sGather.setRegister(mManuObj.getRegister());
           sGather.setRegisterTime(mManuObj.getRegisterTime());
           sGather.setChecker(mManufacture.getChecker());
           sGather.setCheckTime(mManufacture.getCheckTime());
           this.sGatherMapper.insertGather(sGather);
           SGatherDetails sGatherDetails=new SGatherDetails();
           sGatherDetails.setParentId(sGather.getId());
           sGatherDetails.setProductId(dFile.getProductId());
           sGatherDetails.setProductName(dFile.getProductName());
           sGatherDetails.setProductDescribe(dFile.getProductDescribe());
           sGatherDetails.setAmount(mManufacture.getTestedAmount());
           sGatherDetails.setAmountUnit(dFile.getAmountUnit());
           sGatherDetails.setCostPrice(dFile.getCostPrice());
           sGatherDetails.setSubtotal(dFile.getCostPrice()*mManufacture.getTestedAmount());
           this.sGatherMapper.insertgatherDetails(sGatherDetails);
           this.mProceduringMapper.updateProcedure_transfer_tag(mManufacture.getProductId(),mManufacture.getProductName());
       }catch (Exception e){
           e.printStackTrace();
           return 0;
       }

        return 1;
    }
}
